//
// Created by DengLibin on 2019/1/23 0023.
//

#include <iostream>
#include <vector>
using  namespace std;
struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
/**
 * 二叉树展开为链表
 */
TreeNode * last = NULL;
void flatten(TreeNode* root) {
    if(root == NULL){
        return;
    }
    TreeNode * tempNode = root->right;
    if(last !=  NULL){
        last->right = root;
    }
    last = root;

    flatten(root->left);
    flatten(tempNode);
    root->left = NULL;
}

int main75(){

    TreeNode node1 = {3};
    TreeNode node2 = {9};
    TreeNode node3 = {20};
    node1.left = &node2;
    node1.right = &node3;
    TreeNode node5 = {7};
    TreeNode node6 = {15};
    node3.left = &node5;
    node3.right = &node6;
    flatten(&node1);
    return 0;
}
